<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Article extends CI_Controller {

    var $role;
    var $user_id;

    public function __construct() {
        parent::__construct();
        $this->load->model('articles');
        $this->load->helper('url');
        $this->load->library('session');
        $this->role = $this->session->userdata('role');
        $this->user_id = $this->session->userdata('user_id');
        $this->_authenticate();
    }

    public function index() {
        $this->_error();
    }

    private function _error() {
        show_404();
    }

    //AUTHENTICATION//

    private function _authenticate() {
        if ($this->session->userdata('islogged') == null) {
            redirect(base_url() . 'cp/login');
        }
    }

    private function _authorize($user_id) {
        if (($this->user_id != $user_id) && ($this->role != 0)) {
            $this->_error();
        }
    }

    //BASIC FUNCTION//

    public function add() {
        $data['title'] = $this->input->post('ftitle');
        $data['content'] = $this->input->post('fcontent');
        $data['category_id'] = $this->input->post('fcategory');
        $data['published'] = $this->input->post('fpublished');
        $data['author_id'] = $this->session->userdata('user_id');

        //image upload
        $this->load->library('upload');
        if (!$this->upload->do_upload('fimage-file')) {
            $this->session->set_flashdata('article', 'Add Article Failed, Check Your Uploaded Image');
            $this->session->set_flashdata('ftitle',$data['title']);
            $this->session->set_flashdata('fcontent',$data['content']);
        } else {
            $data['image'] = $this->upload->data();
            $data['image_title'] = $this->input->post('fimage-title');
            $this->articles->insert($data);
            $this->session->set_flashdata('article', 'Article Added');
        }
        redirect(base_url() . 'cp');
    }

    public function edit() {
        $article_id = $this->session->userdata('edit_article_id');
        $result = $this->articles->get_article_by_id($article_id);
        if (($result->author_id != $this->user_id) && ($this->role != 0)) {
            $this->_error();
        }
        $data['title'] = $this->input->post('ftitle');
        $data['content'] = $this->input->post('fcontent');
        $data['category_id'] = $this->input->post('fcategory');
        $data['published'] = $this->input->post('fpublished');
        
        //image upload
        $this->load->library('upload');
        if (!$this->upload->do_upload('fimage-file')) {
            $image = $this->upload->data();
            if($image['file_name'] != null){
                $this->session->set_flashdata('article', $this->upload->display_errors('',''));
            } else {
                //nothing uploaded
                $this->articles->update_by_id($article_id, $data);
                $this->session->set_flashdata('article', 'Article Edited');
            }
        } else {
            //upload success
            $data['image'] = $this->upload->data();
            $data['image_title'] = $this->input->post('fimage-title');
            $this->articles->update_by_id($article_id, $data);
            $this->session->set_flashdata('article', 'Image Added');
        }
        redirect(base_url() . 'cp/edit_article/'.$article_id);
    }
    
    public function edit_image(){
        $image_id = $this->session->userdata('edit_image_id');
        $article_id = $this->session->userdata('edit_article_id');
        if($article_id == null){
            $this->_error();
        }
        $result = $this->articles->get_article_by_image_id($image_id);
        if (($result->author_id != $this->user_id) && ($this->role != 0)) {
            $this->_error();
        }
        $data['image_title'] = $this->input->post('fimage-title');
        $this->articles->update_image_by_id($image_id,$data);
        $this->session->set_flashdata('article', 'Image Edited');
        redirect(base_url() . 'cp/edit_article/'.$article_id);
    }

    public function delete($article_id) {
        $result = $this->articles->get_article_by_id($article_id);
        if (($result->author_id != $this->user_id) && ($this->role != 0)) {
            $this->_error();
        }
        $this->articles->delete_by_id($article_id);
        $this->session->set_flashdata('article', 'Article Deleted');
        redirect(base_url() . 'cp/list_article');
    }
    
    public function delete_image($image_id){
        $result = $this->articles->get_article_by_image_id($image_id);
        if (($result->author_id != $this->user_id) && ($this->role != 0)) {
            $this->_error();
        }
        $this->articles->delete_image_by_id($image_id);
        $this->session->set_flashdata('article', 'Image Deleted');
        redirect(base_url() . 'cp/edit_article/'.$this->session->userdata('edit_article_id'));
    }

}

?>